package com.example.springblog.controller;

import com.example.springblog.constant.Constants;
import com.example.springblog.model.BlogInfo;
import com.example.springblog.model.UserInfo;
import com.example.springblog.service.BlogService;
import com.example.springblog.service.UserService;
import com.example.springblog.utils.JwtUtils;
import io.jsonwebtoken.lang.Strings;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private UserService userService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        log.info("获取博客列表..");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("getBlogDetail,blogId:{}", blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);

        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户ID
        Integer userId = JwtUtils.getIdByToken(token);
        //判断作者是否为登录用户
        if (userId!=null && userId==blogDetail.getUserId()){
            blogDetail.setLoginUser(true);
        }else {
            blogDetail.setLoginUser(false);
        }
        return blogDetail;
    }

    @RequestMapping("/add")
    /*public String addBlog(String title, String content) {}*/
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("用户端请求添加博客,blogInfo:{}",blogInfo);

        //参数校验（检查要添加的博客blogInfo属性对不对，这里是客户端传来的参数）
        if(!Strings.hasLength(blogInfo.getTitle()) || !Strings.hasLength(blogInfo.getContent())){
            return false;
        }

        //获取用户信息（检查用户是否登录了）
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //获取登录用户的ID
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId == 0){//要熟悉自己的代码，方便使用，方便知晓返回情况和含义
            log.info("用户未登录");
            return false;
        }
        blogInfo.setUserId(userId);
        return blogService.insertBlog(blogInfo);
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客,blogInfo:{}",blogInfo);
        //参数校验
        if(blogInfo == null
           ||!Strings.hasLength(blogInfo.getTitle())
           ||!Strings.hasLength(blogInfo.getContent())){
            return false;
        }
        blogService.update(blogInfo);
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客，blogId={}",blogId);
        if(blogId <= 0){
            return false;
        }
         BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);//逻辑删除
        blogService.update(blogInfo);
        return true;
    }


    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request){
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId <= 0){
            return null;
        }
        UserInfo userInfo = userService.selectById(userId);
        return userInfo;
    }

}
